package com.dingwen.spsest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dingwen.spsest.entity.MenuEntity;
import com.dingwen.spsest.entity.RoleEntity;
import com.dingwen.spsest.entity.UserEntity;
import com.dingwen.spsest.mapper.MenuMapper;
import com.dingwen.spsest.mapper.RoleMapper;
import com.dingwen.spsest.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * user service impl
 *
 * @author dingwen
 * 2021.05.18 11:15
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {


    /**
     * 用户映射器
     */
    private final UserMapper userMapper;

    /**
     * 角色映射器
     */
    private final RoleMapper roleMapper;

    /**
     * 菜单映射器
     */
    private final MenuMapper menuMapper;


    @Autowired
    public UserDetailsServiceImpl(UserMapper userMapper,
                                  RoleMapper roleMapper,
                                  MenuMapper menuMapper) {
        this.userMapper = userMapper;
        this.roleMapper = roleMapper;
        this.menuMapper = menuMapper;
    }

    /**
     * 根据用户名查询用户
     *
     * @param username 用户名
     * @return {@link UserDetails}
     * @throws UsernameNotFoundException 找不到用户
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        UserEntity userEntity = userMapper.selectOne(
                new QueryWrapper<UserEntity>()
                        .eq("username", username)
        );

        Optional.ofNullable(userEntity).orElseThrow(() -> new UsernameNotFoundException("用户不存在"));

//        List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList("admin");

        // 获取用户角色、菜单列表
        List<RoleEntity> roleEntityList = roleMapper.selectRoleByUserId(userEntity.getId());
        List<MenuEntity> menuEntityList = menuMapper.selectMenuByUserId(userEntity.getId());
        // 权限列表
        List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();


        roleEntityList.forEach(roleEntity -> {
                    grantedAuthorityList.add(new SimpleGrantedAuthority("ROLE_" + roleEntity.getName()));
                }
        );


        menuEntityList.forEach(menuEntity -> {
            grantedAuthorityList.add(new SimpleGrantedAuthority(menuEntity.getPermission()));
        });


        return User
                .withUsername(userEntity.getUsername())
                .password(userEntity.getPassword())
                .authorities(grantedAuthorityList).build();
    }
}
